<script lang="ts">
export default {
  name: "RepoCompEntityTable"
}
</script>

<template>
  <!-- 数据表单 -->
  <el-table
      ref="dataTableRef"
      v-loading="false"
      :data="dataState.entityList"
      border
      highlight-current-row
  >
    <!-- 展开列表 -->
    <el-table-column type="expand">
      <template #default="props">
        <el-table
            ref="dataTableRef"
            :data="props.row.operates"
            border
            highlight-current-row
        >
          <el-table-column align="center" label="操作名称" min-width="100" prop="operateName"/>
          <el-table-column align="center" label="操作模式" min-width="100" prop="operateMode"/>
          <el-table-column align="center" label="业务类型" min-width="100" prop="serviceType"/>
          <el-table-column align="center" label="返回类型" min-width="100" prop="dataType"/>
          <el-table-column align="center" label="操作" width="160">
            <template #default="scope">
              <el-button :icon="ArrowRight" circle plain type="primary" @click.stop="handleDetailPage(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-table-column>

    <!-- 列表 -->
    <el-table-column label="设备厂商" min-width="250" prop="manufacturer"/>
    <el-table-column label="设备类型" min-width="250" prop="deviceType"/>
    <el-table-column label="作者" min-width="250" prop="author"/>
    <el-table-column label="描述信息" min-width="250" prop="description"/>
    <el-table-column :formatter="formatDate4elTableColumn" label="版本日期" prop="updateTime" width="250"/>


  </el-table>

  <!-- 分页工具条 -->
  <pagination v-if="dataState.total > 0" v-model:limit="dataState.queryParams.pageSize"
              v-model:page="dataState.queryParams.pageNum" :total="dataState.total"
              @pagination="handleQuery"/>
</template>

<script lang="ts" setup>
import {onMounted, reactive} from "vue";

import {
  RepoCompScriptVersionEntity,
  RepoCompQueryParam
} from "@/api/cloud/repository/repocomp/script/version/types";
import {
  listRepoCompVersionEntityPages,
} from "@/api/cloud/repository/repocomp/script/version";
import {ArrowRight} from '@element-plus/icons-vue';
import {formatDate4elTableColumn,} from '@/utils/formatter';
import {useRouter} from "vue-router";

const props = defineProps(['modelType'])
const emit = defineEmits(['rowClick', 'more']);// 定义组件的事件通知
const router = useRouter();

/**
 * 对外声明接口：父页面可以调用它
 */
defineExpose({refreshTable});

// 数据状态：定义
let dataState = reactive({
  // 页面转载状态
  loading: false,
  // 选中ID数组
  ids: [],
  // 记录总数
  total: 0,
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 查询参数
  queryParams: {pageNum: 1, pageSize: 10} as RepoCompQueryParam,
  // 父节点的ID
  scriptId: '',
  // 实体数据
  entityList: [] as RepoCompScriptVersionEntity[],
});

/**
 * 刷新页面信息
 */
function handleQuery() {
  emit('rowClick', {});

  dataState.loading = true;

  dataState.entityList = [];
  dataState.queryParams.scriptId = dataState.scriptId;
  listRepoCompVersionEntityPages(dataState.queryParams).then(({data}) => {
    data.list.forEach(value => {
      let item = value;
      for (let operate of item.operates) {
        operate.versionId = value.id;
      }
      dataState.entityList.push(item);
    });


    dataState.total = data.total;
    dataState.loading = false;
  });
}

function handleDetailPage(row: any) {
  router.push('/cloud/repository/jsp-decoder/version/operate?versionId=' + row.versionId + '&operateId=' + row.operateId);
}

/**
 * 刷新表单
 */
function refreshTable(queryParams: RepoCompQueryParam) {
  dataState.queryParams = queryParams;
  if (dataState.queryParams.ownerId === '') {
    dataState.queryParams.ownerId = undefined;
  }
  if (dataState.queryParams.groupName === '') {
    dataState.queryParams.groupName = undefined;
  }
  if (dataState.queryParams.manufacturer === '') {
    dataState.queryParams.manufacturer = undefined;
  }
  if (dataState.queryParams.deviceType === '') {
    dataState.queryParams.deviceType = undefined;
  }
  if (dataState.queryParams.keyword === '') {
    dataState.queryParams.keyword = undefined;
  }

  handleQuery();
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
  let param = location.search.split('?')[1];
  let searchParams = new URLSearchParams(param);
  dataState.scriptId = searchParams.get('scriptId') as string;

  handleQuery();
});

</script>
